﻿// ***********************************************************************
// Project			: Micua
// Assembly         : Micua.Model
// Author           : iceStone
// Created          : 2013-11-23 22:49
//
// Last Modified By : iceStone
// Last Modified On : 2013-11-23 22:49
// ***********************************************************************
// <copyright file="PostView.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary>Post视图模型</summary>
// ***********************************************************************

using System;
using System.Text.RegularExpressions;
using Micua.Model;
using Micua.Model.Extension;
using Micua.Utility;

namespace Micua.UI.Model
{
    /// <summary>
    /// Post视图模型
    /// </summary>
    /// <remarks>
    ///  2013-11-23 22:50 Created By iceStone
    /// </remarks>
    public class PostModel : Post
    {
        /// <summary>
        /// 链接
        /// </summary>
        public string Link { get; set; }
        /// <summary>
        /// 前后文章
        /// </summary>
        public Around<Post> Around { get; set; }        
        //public Around<Post> Around(IPostService service)
        //{
        //    var around = service.GetPostAround(this);
        //    return around;
        //}

        #region 文章缩略图 +string Image(string def)
        /// <summary>
        /// 文章缩略图
        /// </summary>
        public string Image(string def)
        {
            Match match = Regex.Match(Content, "<img.+?src=\"(.+?)\".+?>");
            if (match.Length > 0)
                def = match.Groups[1].Value;
            return def;
        }
        #endregion

        #region 文章概要 +string Note(int length)
        /// <summary>
        /// 文章概要
        /// </summary>
        /// <param name="length">最大长度</param>
        /// <returns>文章概要</returns>
        public string Note(int length)
        {
            return Note(length, "...");
        }
        #endregion

        #region 文章概要 string Note(int length, string moreFlag)
        /// <summary>
        /// 文章概要
        /// </summary>
        /// <param name="length">最大长度</param>
        /// <param name="moreFlag">更多标识</param>
        /// <returns>文章概要</returns>
        public string Note(int length, string moreFlag)
        {
            if (!String.IsNullOrEmpty(Excerpt)) return Excerpt;
            var temp = Content.TrimHtml();
            return temp.Length() > length ? temp.CutString(length) + moreFlag : temp;
        }
        #endregion

        /// <summary>
        /// 获取视图模型
        /// </summary>
        /// <param name="post">实体模型</param>
        /// <returns>视图模型</returns>
        public static PostModel GetViewModel(Post post)
        {
            var model = new PostModel
            {
                Id = post.Id,
                AuthorName = post.AuthorName,
                Categorys = post.Categorys,
                Tags = post.Tags,
                Content = post.Content,
                CommentCount = post.CommentCount,
                CommentStatus = post.CommentStatus,
                Excerpt = post.Excerpt,
                IsTop = post.IsTop,
                Modified = post.Modified,
                PubDate = post.PubDate,
                ParentId = post.ParentId,
                Password = post.Password,
                Slug = post.Slug,
                Title = post.Title,
                User = post.User,
                UserId = post.UserId,
                ViewCount = post.ViewCount,
                
            };
            return model;
        }
    }
}
